package com.haibei.view.web;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;

/**
 * 自定义webview类
 * 
 * @author
 * 
 */
public class WuyouWebview extends WebView {
	/**
	 * 用于监听消息loading框的自定义listener
	 */
	private OnContentShowListener mOnContentShowListener;
	/**
	 * 用于监听加载网页出错的listener
	 */
	private OnErrorListener mOnErrorListener;
	/**
	 * 用于监听网页加载成功的listener
	 */
	private OnPageFinishedListener mOnPageFinishedListener;
	/**
	 * 用于监听网页加载成功的listener
	 */
	private OnSuccessFinishedListener mOnPageSuccessFinishedListener;
	/**
	 * 用于监听网页开始加载的listener
	 */
	private OnPageStartListener mOnPageStartListener;
	/**
	 * 用于调用loadurl时监听处理一些事务
	 */
	/*
	 * private OnPreLoadListener mOnPreLoadListener;
	 */

	private boolean mInTouching;

	private String url;

	private int mTouchSlop;

	public int getTouchSlop() {
		return mTouchSlop;
	}

	public void setTouchSlop(int touchSlop) {
		this.mTouchSlop = touchSlop;
	}

	public float[] mLastPosition = { 0.0f, 0.0f };

	private void init() {
		this.setScrollBarStyle(SCROLLBARS_INSIDE_OVERLAY);
	}

	public void updatePosition(MotionEvent ev) {
		this.mLastPosition[0] = ev.getX();
		this.mLastPosition[1] = ev.getY();
	}

	public WuyouWebview(Context context) {
		super(context);
		/* 让进度条显示在网页内容之上一层而不是和内容平行 */
		init();
	}

	public WuyouWebview(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public WuyouWebview(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	@Deprecated
	public interface OnContentShowListener {
		public void onContentShow(WebView view);
	}

	@Deprecated
	public void setOnContentShowListener(OnContentShowListener listener) {
		mOnContentShowListener = listener;
	}

	@Deprecated
	public OnContentShowListener getContentShowListener() {
		return mOnContentShowListener;
	}

	@Deprecated
	public void contentShow() {
		if (mOnContentShowListener != null)
			mOnContentShowListener.onContentShow(WuyouWebview.this);
	}

	public interface OnSuccessFinishedListener {
		public void onSuccessFinished(WebView view);
	}

	public interface OnErrorListener {
		public void onError(WebView view);
	}

	public interface OnPageStartListener {
		public void onPageStart(WebView view);
	}

	public interface OnPageFinishedListener {
		public void onPageFinished(WebView view);
	}

	/*
	 * public interface OnPreLoadListener{ public void onPreLoad(WebView view);
	 * }
	 */

	public void setOnErrorListener(OnErrorListener listener) {
		mOnErrorListener = listener;
	}

	public void setPageOnFinishedListener(OnPageFinishedListener listener) {
		mOnPageFinishedListener = listener;
	}

	/*
	 * public void setOnPreLoadListener(OnPreLoadListener listener){
	 * mOnPreLoadListener = listener; }
	 */

	public OnPageStartListener getOnPageStartLinstener() {
		return this.mOnPageStartListener;
	}

	public void setOnPageStartLinstener(OnPageStartListener listener) {
		this.mOnPageStartListener = listener;
	}

	public void setOnSuccessFinishedListener(OnSuccessFinishedListener listener) {
		this.mOnPageSuccessFinishedListener = listener;
	}

	public void contentLoadError() {
		if (mOnErrorListener != null) {
			mOnErrorListener.onError(WuyouWebview.this);
		}
	}

	public void pageFinish() {
		if (mOnPageFinishedListener != null) {
			mOnPageFinishedListener.onPageFinished(WuyouWebview.this);
		}
	}

	public void pageSuccess() {
		if (mOnPageSuccessFinishedListener != null) {
			mOnPageSuccessFinishedListener.onSuccessFinished(WuyouWebview.this);
		}
	}

	/*
	 * public void preLoadStart(){ if(mOnPreLoadListener != null){
	 * mOnPreLoadListener.onPreLoad(MyWebView.this); } }
	 */

	@Override
	public boolean performLongClick() {
		return false;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public void scrollTo(int x, int y) {
		if (!mInTouching) {
			if (y <= 2 && getScrollY() > 2) {
				return;
			}
		}
		super.scrollTo(x, y);
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		final int action = ev.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			mInTouching = true;
			break;
		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			mInTouching = false;
			break;
		}
		return super.onTouchEvent(ev);
	}

	public int computeVertical() {
		return computeVerticalScrollOffset();
	}

}